package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.bean.Setmeal;
import com.zrrd.reggie.bean.SetmealDish;
import com.zrrd.reggie.bean.dto.SetmealDto;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.service.SetmealDishService;
import com.zrrd.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    //分页查套餐
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        if (name != null && !name.equals("")) {
            wrapper.like("name", name);
        }
        setmealService.page(pageInfo, wrapper);
        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(dtoPage, pageInfo, "records");
        List<Setmeal> setmealList = pageInfo.getRecords();
        List<SetmealDto> dtoList = new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            dtoList.add(setmealDto);
        }
        dtoPage.setRecords(dtoList);
        return R.success(dtoPage);
    }

    //添加套餐
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto dto) {
        setmealService.saveSetmeanWithDish(dto);
        return R.success("添加成功");
    }

    //根据id获取套餐信息
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealWithDish(@PathVariable Long id) {
        return R.success(setmealService.getSetmealWithDish(id));
    }

    //修改套餐信息
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto dto) {
        return setmealService.updateSetmealWithDish(dto) ? R.success("修改成功") : R.error("修改失败");
    }

    //修改套餐售卖状态
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping("/status/{status}")
    public R<String> updateSetmealStatus(@PathVariable("status") Integer status, Long[] ids) {
        List<Setmeal> setmealList = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealList.add(setmeal);
        }
        return setmealService.updateBatchById(setmealList) ? R.success("修改套餐状态成功") : R.error("修改套餐状态失败");
    }

    //删除套餐
    @CacheEvict(value = "setmealCache", allEntries = true)
    @DeleteMapping
    public R<String> deleteSetmeals(Long[] ids) {
        return setmealService.deleteSetmeals(ids) ? R.success("删除套餐成功") : R.error("删除套餐失败");
    }

    //根据分类id查询套餐
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List> list(Setmeal setmeal) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", setmeal.getCategoryId());
        wrapper.eq("status", setmeal.getStatus());
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }

    //根据套餐id查询关联的菜品关系数据
    @GetMapping("/dish/{id}")
    public R<List> getDishListById(@PathVariable("id") Long id) {
        //
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);
        List<SetmealDish> setmealdishList = setmealDishService.list(wrapper);
        return R.success(setmealdishList);
    }
}
